import mysql.connector

# MySQL 连接
conn = mysql.connector.connect(
    host="localhost",
    port=3306,
    user="root",
    password="123456",
    database="python",
    auth_plugin="mysql_native_password"  # 指定认证插件
)
mycursor = conn.cursor()
sql = 'INSERT INTO `user` (`name`,age) VALUES(%s, %s)'
# 插入一条数据
# val = ('张三', 20)
# mycursor.execute(sql, val)
# conn.commit()
# print(mycursor.rowcount, '记录插入成功')
# 批量插入
# vals = [
#     ('李四', 31),
#     ('王五', 34),
#     ('赵六', 22),
#     ('吴思', 25),
#     ('将路', 26),
#     ('罗是', 18),
# ]
# mycursor.executemany(sql, vals)
# conn.commit()
# print(mycursor.rowcount, "条数据插入成功")

# 修改
# sql = 'UPDATE `user` set `name`="西子" WHERE id=7'
# mycursor.execute(sql)
# conn.commit()
# print(mycursor.rowcount, "条修改成功")

# 删除
# sql = 'DELETE FROM `user` WHERE id=7'
# mycursor.execute(sql)
# conn.commit()
# print(mycursor.rowcount, "条删除成功")

# 查询
sql = 'SELECT * FROM `user`'
mycursor.execute(sql)
result = mycursor.fetchall()
for item in result:
    print(item)
